package com.wzy.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzy.base.exception.CustomException;
import com.wzy.ucenter.feignclint.CheckCodeClient;
import com.wzy.ucenter.mapper.XcUserMapper;
import com.wzy.ucenter.model.dto.FindPasswordDto;
import com.wzy.ucenter.model.po.XcUser;
import com.wzy.ucenter.service.FindPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author wzy
 * @Date 2024/2/1 16:26
 * @description: 找回密码业务实现
 */
@Service
public class FindPasswordServiceImpl implements FindPasswordService {
    @Autowired
    private CheckCodeClient checkCodeClient;
    @Autowired
    private XcUserMapper userMapper;
    /**
     * 找回密码
     * @param findPasswordDto 前端找回密码相关参数
     */
    @Override
    public void FindPassword(FindPasswordDto findPasswordDto) {
        String email = findPasswordDto.getEmail();
        String checkcode = findPasswordDto.getCheckcode();
        Boolean verify = checkCodeClient.verify(email, checkcode);
        if (!verify) {
            throw new CustomException("验证码输入错误");
        }
        String password = findPasswordDto.getPassword();
        String confirmpwd = findPasswordDto.getConfirmpwd();
        if (!password.equals(confirmpwd)) {
            throw new CustomException("两次输入的密码不一致");
        }
        LambdaQueryWrapper<XcUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(XcUser::getEmail, findPasswordDto.getEmail());
        XcUser user = userMapper.selectOne(lambdaQueryWrapper);
        if (user == null) {
            throw new CustomException("用户不存在");
        }
        user.setPassword(new BCryptPasswordEncoder().encode(password));
        userMapper.updateById(user);
    }
}
